package yui.bss.demo.controller;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import yui.bss.demo.dto.SysUserDto;
import yui.bss.demo.mgr.SysUserMgr;
import yui.bss.demo.vo.SysUserVo;
import yui.comn.hub.utils.DateUtils;
import yui.comn.hub.utils.HubStringUtils;
import yui.comn.mybatisx.core.conditions.Wrapper;
import yui.comn.mybatisx.core.conditions.query.FindWrapper;
import yui.comn.mybatisx.core.conditions.update.UpdateWrapper;
import yui.comn.mybatisx.core.toolkit.EntityUtils;

/**
 * <p>
 * 系统用户
 * </p>
 *
 * @author yui
 */
@Api(value="系统用户")
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserMgr sysUserMgr;

    @ApiOperation(value = "更新4")
    @GetMapping("upd4")
    public Object upd4() {
        List<SysUserVo> voList= new ArrayList<>();
        for (int i = 1; i < 11; i++) {
            SysUserVo vo = new SysUserVo();
            vo.setId((long) i);
            vo.setUsername(i + "--11");
            vo.setEmail(i + "--22");
            vo.setRmks(i + "--33");
            vo.setType(null);
            voList.add(vo);
        }

        sysUserMgr.updateAllBatchById(voList);
        return buildSuccess();
    }

    @ApiOperation(value = "更新3")
    @GetMapping("upd3")
    public Object upd3() {
        UpdateWrapper<SysUserVo> updateWrapper1 = new UpdateWrapper<>();

        updateWrapper1.lambda().eq(SysUserVo::getId, 1)
                .set(SysUserVo::getUsername, "111")
                .set(SysUserVo::getEmail, null)
                .set(SysUserVo::getRmks, "11111111");

        UpdateWrapper<SysUserVo> updateWrapper2 = new UpdateWrapper<>();
        updateWrapper2.eq("id", 2) // 设置更新条件，这里以id为条件
                .set("username", "22222")
                .set("email", null)
                .set("rmks", "null");

        UpdateWrapper<SysUserVo> updateWrapper3 = new UpdateWrapper<>();
        updateWrapper3.eq("id", 3) // 设置更新条件，这里以id为条件
                .set("username", "3333yy")
                .set("email", "3333uu")
                .setTableName("t_sys_user3");

        List<Wrapper<SysUserVo>> list = new ArrayList<>();
        list.add(updateWrapper1);
        list.add(updateWrapper2);
        list.add(updateWrapper3);

        // sysUserMgr.updateBatch(list);
        sysUserMgr.update(null, updateWrapper3);
        return buildSuccess();
    }


//    @ApiOperation(value = "列表查询")
//    @GetMapping("upd2")
//    public Object upd2(String query) {
//
//        return build(mapIPage);
//    }

    @ApiOperation(value = "列表查询")
    @GetMapping("pageMaps")
    public Object pageMaps(String query) {
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        fw.select(" * ");
        fw.setTableName("t_sys_user2");
        IPage<Map<String, Object>> mapIPage = sysUserMgr.pageMaps(fw);
        return build(mapIPage);
    }

    @ApiOperation(value = "更新")
    @GetMapping("updEntityToWrapper2")
    public Object updEntityToWrapper2() {
        List<SysUserVo> voList= new ArrayList<>();
        for (int i = 1; i < 11; i++) {
            SysUserVo vo = new SysUserVo();
            vo.setId((long) i);
            vo.setUsername(i + "--1");
            vo.setEmail(i + "--2");
            vo.setRmks(i + "--3");
            vo.setType(null);
            voList.add(vo);
        }

        sysUserMgr.updateBatch(voList, "username", "email", "type");
        return buildSuccess();
    }

    @ApiOperation(value = "更新")
    @GetMapping("updEntityToWrapper")
    public Object updEntityToWrapper() {
        List<SysUserVo> voList= new ArrayList<>();
        for (int i = 1; i < 11; i++) {
            SysUserVo vo = new SysUserVo();
            vo.setId((long) i);
            vo.setUsername(i + "--1");
            vo.setEmail(i + "--2");
            vo.setRmks(i + "--3");
            vo.setType(null);
            voList.add(vo);
        }

        sysUserMgr.updateBatch(getList(voList, "username", "email", "type"));
        return buildSuccess();
    }

    private List<Wrapper<SysUserVo>> getList(List<SysUserVo> entityList, String... args) {
        List<Wrapper<SysUserVo>> list = new ArrayList<>();
        try {
            entityList.forEach(vo -> {
                Class<?> clazz = vo.getClass();
                UpdateWrapper<SysUserVo> wrapper = new UpdateWrapper<>();
                wrapper.eq("id", vo.getId());

                for (String arg : args) {
                    try {
                    Method method = clazz.getMethod(HubStringUtils.getGetMethodName(arg));
                    Object valObj = method.invoke(vo);
                    wrapper.set(arg, valObj);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }

                list.add(wrapper);
            });
        } catch (Exception e) {
            String errMsg = "获取方法值失败";
            throw new RuntimeException(errMsg, e);
        }
        return list;
    }

    @ApiOperation(value = "更新")
    @GetMapping("updWrapper")
    public Object updWrapper() {
        UpdateWrapper<SysUserVo> updateWrapper1 = new UpdateWrapper<>();

        updateWrapper1.lambda().eq(SysUserVo::getId, 1)
                .set(SysUserVo::getUsername, "111")
                .set(SysUserVo::getEmail, null)
                .set(SysUserVo::getRmks, "11111111");

        UpdateWrapper<SysUserVo> updateWrapper2 = new UpdateWrapper<>();
        updateWrapper2.eq("id", 2) // 设置更新条件，这里以id为条件
                .set("username", "22222")
                .set("email", null)
                .set("rmks", "null");

        UpdateWrapper<SysUserVo> updateWrapper3 = new UpdateWrapper<>();
        updateWrapper3.eq("id", 3) // 设置更新条件，这里以id为条件
                .set("username", "3333")
                .set("email", 3333);

        List<Wrapper<SysUserVo>> list = new ArrayList<>();
        list.add(updateWrapper1);
        list.add(updateWrapper2);
        list.add(updateWrapper3);

        sysUserMgr.updateBatch(list);
        return buildSuccess();
    }

    @ApiOperation(value = "更新")
    @GetMapping("upd")
    public Object upd() {
        UpdateWrapper<SysUserVo> updateWrapper = new UpdateWrapper<>();

//        updateWrapper.lambda().eq(SysUserVo::getId, 1)
//                        .set(SysUserVo::getUsername, "hhhh")
//                        .set(SysUserVo::getEmail, null);
//        updateWrapper.eq("id", 1) // 设置更新条件，这里以id为条件
//                .set("username", "dddd")
//                .set("email", null); //
//        updateWrapper.lambda().eq(SysUserVo::getId, 1);

        SysUserVo userVo = new SysUserVo();
        userVo.setId(1L);
        userVo.setUsername("kkkk");
        userVo.setEmail(null);

        updateWrapper.setEntity(userVo);

        sysUserMgr.update(userVo, updateWrapper);

        return buildSuccess();
    }

    @ApiOperation(value = "批量新增")
    @GetMapping("addBatch")
    public Object addBatch() {
        List<SysUserVo> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            SysUserVo user = new SysUserVo();
            user.setUsername("u1-" + i);
            list.add(user);
        }
        sysUserMgr.addBatch(list);

        return buildSuccess();
    }

    @ApiOperation(value = "批量更新")
    @GetMapping("updALLBatch")
    public Object updALLBatch(String email) {
        List<SysUserVo> list = sysUserMgr.listVo();
        list.forEach(user -> {
            user.setEmail("updALLBatch:" + email);
        });

        sysUserMgr.updateAllBatchById(list);

        return buildSuccess();
    }

    @ApiOperation(value = "批量更新")
    @GetMapping("updBatch")
    public Object updBatch(String email) {
        List<SysUserVo> list = sysUserMgr.listVo();
        list.forEach(user -> {
            user.setEmail("updBatch" + email);
        });

        sysUserMgr.updateBatchById(list);

        return buildSuccess();
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Object page(String query) { 
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        IPage<SysUserDto> page = sysUserMgr.page(fw);
        return build(page, "PAGE"); 
    }

    @ApiOperation(value = "列表查询")
    @GetMapping("list")
    public Object list(String query) { 
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        List<SysUserDto> list = sysUserMgr.list(fw);
        return build(list, "LIST"); 
    }

    @ApiOperation(value = "list2")
    @GetMapping("list2")
    public Object list2(String query) { 
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        List<SysUserDto> list = sysUserMgr.list(fw);
        return build(list, "LIST2"); 
    }

    @ApiOperation(value = "listUserARole")
    @GetMapping("listUserARole")
    public Object listUserARole(String query) { 
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        List<SysUserDto> list = sysUserMgr.listUserARole(fw);
        return buildList(list, "USER_ROLE"); 
    }

    @ApiOperation(value = "listUserARole2")
    @GetMapping("listUserARole2")
    public Object listUserARole2(String query) { 
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        List<SysUserDto> list = sysUserMgr.listUserARole(fw);
        return buildList(list, "USER_ROLE2"); 
    }

    @ApiOperation(value = "listUserLtRole")
    @GetMapping("listUserLtRole")
    public Object listUserLtRole(String query) { 
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        List<SysUserDto> list = sysUserMgr.listUserLtRole(fw);
        return buildList(list, "USER_ROLE"); 
    }

    @ApiOperation(value = "listUserWRole")
    @GetMapping("listUserWRole")
    public Object listUserWRole(String query) { 
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        List<SysUserDto> list = sysUserMgr.listUserWRole(fw);
        return buildList(list, "USER_W_ROLE"); 
    }

    @ApiOperation(value = "listUserWRole2")
    @GetMapping("listUserWRole2")
    public Object listUserWRole2(String query) { 
        FindWrapper<SysUserVo> fw = getWrapper(query, SysUserVo.class);
        List<SysUserDto> list = sysUserMgr.listUserWRole(fw);
        return buildList(list, "USER_W_ROLE2"); 
    }   

}